package com.ruoyi.monitor.service.impl;

import java.util.*;

import com.ruoyi.base.domain.BussinessItemVo;
import com.ruoyi.base.service.PublicToolsService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.monitor.mapper.BusWaterIntegrationmonitordataMapper;
import com.ruoyi.monitor.domain.BusWaterIntegrationmonitordata;
import com.ruoyi.monitor.service.IBusWaterIntegrationmonitordataService;
import org.springframework.util.CollectionUtils;

/**
 * 手工断面监测数据（月）Service业务层处理
 * 
 * @author maliang
 * @date 2025-08-16
 */
@Service
public class BusWaterIntegrationmonitordataServiceImpl implements IBusWaterIntegrationmonitordataService 
{
    @Autowired
    private BusWaterIntegrationmonitordataMapper busWaterIntegrationmonitordataMapper;
    @Autowired
    private PublicToolsService publicToolsService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询手工断面监测数据（月）
     * 
     * @param segmentid 手工断面监测数据（月）主键
     * @return 手工断面监测数据（月）
     */
    @Override
    public BusWaterIntegrationmonitordata selectBusWaterIntegrationmonitordataBySegmentid(String segmentid)
    {
        return busWaterIntegrationmonitordataMapper.selectBusWaterIntegrationmonitordataBySegmentid(segmentid);
    }

    /**
     * 查询手工断面监测数据（月）列表
     * 
     * @param busWaterIntegrationmonitordata 手工断面监测数据（月）
     * @return 手工断面监测数据（月）
     */
    @Override
    public List<BusWaterIntegrationmonitordata> selectBusWaterIntegrationmonitordataList(BusWaterIntegrationmonitordata busWaterIntegrationmonitordata)
    {
        return busWaterIntegrationmonitordataMapper.selectBusWaterIntegrationmonitordataList(busWaterIntegrationmonitordata);
    }

    /**
     * 新增手工断面监测数据（月）
     * 
     * @param busWaterIntegrationmonitordata 手工断面监测数据（月）
     * @return 结果
     */
    @Override
    public int insertBusWaterIntegrationmonitordata(BusWaterIntegrationmonitordata busWaterIntegrationmonitordata)
    {
        return busWaterIntegrationmonitordataMapper.insertBusWaterIntegrationmonitordata(busWaterIntegrationmonitordata);
    }

    /**
     * 修改手工断面监测数据（月）
     * 
     * @param busWaterIntegrationmonitordata 手工断面监测数据（月）
     * @return 结果
     */
    @Override
    public int updateBusWaterIntegrationmonitordata(BusWaterIntegrationmonitordata busWaterIntegrationmonitordata)
    {
        return busWaterIntegrationmonitordataMapper.updateBusWaterIntegrationmonitordata(busWaterIntegrationmonitordata);
    }

    /**
     * 批量删除手工断面监测数据（月）
     * 
     * @param segmentids 需要删除的手工断面监测数据（月）主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterIntegrationmonitordataBySegmentids(String[] segmentids)
    {
        return busWaterIntegrationmonitordataMapper.deleteBusWaterIntegrationmonitordataBySegmentids(segmentids);
    }

    /**
     * 删除手工断面监测数据（月）信息
     * 
     * @param segmentid 手工断面监测数据（月）主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterIntegrationmonitordataBySegmentid(String segmentid)
    {
        return busWaterIntegrationmonitordataMapper.deleteBusWaterIntegrationmonitordataBySegmentid(segmentid);
    }

    @Override
    public Map yearPollutantsPropor(String year, String countyCode, String riverId) {
        Map resMap = new LinkedHashMap();
        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",year+"-01-01 00:00:00");
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",year+"-12-31 00:00:00");
        //超标日前三
        List<Map> cbThreeDay = busWaterIntegrationmonitordataMapper.cbThreeMonth(startDate,endDate,countyCode,riverId);
        resMap.put("cbThreeDay",cbThreeDay);
        //超标前三因子
        List<Map> cbThreeItem = busWaterIntegrationmonitordataMapper.cbThreeItem(startDate,endDate,countyCode,riverId);
        resMap.put("cbThreeItem",cbThreeItem);

        //超标因子
        //检测因子
        List<String> itemNameList = new ArrayList<>();
        BussinessItemVo vo = new BussinessItemVo();
        vo.setBussinessCategoryCode("MSV");
        List<Map<String,Object>> itemList=publicToolsService.bussinessItemData(vo);
        for (Map<String, Object> stringObjectMap : itemList) {
            itemNameList.add(StringUtils.getString(stringObjectMap.get("itemName")));
        }

        List<String> dayList = new ArrayList<>();

        List<List<String>> dataList = new ArrayList<>();
        List<Map> cbCountByDayAndItem = busWaterIntegrationmonitordataMapper.cbCountByMonthAndItem(startDate,endDate,countyCode,riverId);
        while(!startDate.after(endDate)){
            String day = DateUtils.parseDateToStr("yyyy-MM",startDate);
            dayList.add(day);

            List<String> list = new ArrayList<>();
            for (Map<String, Object> stringObjectMap : itemList) {
                String item = StringUtils.getString(stringObjectMap.get("itemName"));

                String count = "0";
                for (Map map : cbCountByDayAndItem) {
                    String dataTime = StringUtils.getString(map.get("DATATIME"));
                    String itemname = StringUtils.getString(map.get("ITEMNAME"));
                    if(item.equals(itemname) && dataTime.equals(day)){
                        count = StringUtils.getString(map.get("DATACOUNT"));
                        break;
                    }
                }
                list.add(count);
            }
            dataList.add(list);
            startDate = DateUtils.addMonths(startDate,1);
        }

        resMap.put("dayList",dayList);
        resMap.put("itemNameList",itemNameList);
        resMap.put("dataList",dataList);
        return resMap;
    }

    @Override
    public Map monthPollutantsPropor(String startTime, String endTime,String countyCode,String riverId) {
        Map resMap = new LinkedHashMap();
        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",startTime+"-01 00:00:00");
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",DateUtils.getLastDayOfMonth(DateUtils.dateTime("yyyy-MM-dd",endTime+"-01"))+" 23:59:59");
        //超标日前三
        List<Map> cbThreeMonth = busWaterIntegrationmonitordataMapper.cbThreeMonth(startDate,endDate,countyCode,riverId);
        resMap.put("cbThreeMonth",cbThreeMonth);
        //超标前三因子
        List<Map> cbThreeItem = busWaterIntegrationmonitordataMapper.cbThreeItem(startDate,endDate,countyCode,riverId);
        resMap.put("cbThreeItem",cbThreeItem);

        //超标因子
        //检测因子
        List<String> itemNameList = new ArrayList<>();
        BussinessItemVo vo = new BussinessItemVo();
        vo.setBussinessCategoryCode("MSV");
        List<Map<String,Object>> itemList=publicToolsService.bussinessItemData(vo);
        for (Map<String, Object> stringObjectMap : itemList) {
            itemNameList.add(StringUtils.getString(stringObjectMap.get("itemName")));
        }

        List<String> dayList = new ArrayList<>();

        List<List<String>> dataList = new ArrayList<>();
        List<Map> cbCountByDayAndItem = busWaterIntegrationmonitordataMapper.cbCountByMonthAndItem(startDate,endDate,countyCode,riverId);
        while(!startDate.after(endDate)){
            String day = DateUtils.parseDateToStr("yyyy-MM",startDate);
            dayList.add(day);

            List<String> list = new ArrayList<>();
            for (Map<String, Object> stringObjectMap : itemList) {
                String item = StringUtils.getString(stringObjectMap.get("itemName"));

                String count = "0";
                for (Map map : cbCountByDayAndItem) {
                    String dataTime = StringUtils.getString(map.get("DATATIME"));
                    String itemname = StringUtils.getString(map.get("ITEMNAME"));
                    if(item.equals(itemname) && dataTime.equals(day)){
                        count = StringUtils.getString(map.get("DATACOUNT"));
                        break;
                    }
                }
                list.add(count);
            }
            dataList.add(list);
            startDate = DateUtils.addMonths(startDate,1);
        }

        resMap.put("monthList",dayList);
        resMap.put("itemNameList",itemNameList);
        resMap.put("dataList",dataList);
        return resMap;
    }
}
